package com.cysnake.easyyi.activity;

import java.util.ArrayList;

import com.cysnake.easyyi.R;
import com.cysnake.easyyi.Dao.GuaciDao;
import com.cysnake.easyyi.tools.CommonTool;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ZhanbuActivity extends Activity {

	LinearLayout benGuaLay;
	LinearLayout bianGuaLay;

	Button startButton;
	Button detailButton;
	TextView textView;
	TextView benHeadText;
	TextView bianHeadText;

	ImageView benUpImage;
	ImageView benDownImage;
	ImageView bianUpImage;
	ImageView bianDownImage;

	ArrayList<Integer> guaList;
	String benInfo;
	String bianInfo;

	GuaciDao guaDao;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.zhanbu);
		modify();
	}

	private void modify() {
		guaDao = new GuaciDao(ZhanbuActivity.this);
		guaList = new ArrayList<Integer>();

		modifyText();
		modifyImage();
		modifyButton();
		modifyLayout();
	}

	private void modifyLayout() {
		benGuaLay = (LinearLayout) findViewById(R.id.zhanbu_layout_bengua);
		benGuaLay.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ZhanbuActivity.this,
						GuaInfoActivity.class);
				intent.putExtra("name", benInfo);
				startActivity(intent);

			}
		});
		bianGuaLay = (LinearLayout) findViewById(R.id.zhanbu_layout_biangua);
		bianGuaLay.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ZhanbuActivity.this,
						GuaInfoActivity.class);
				intent.putExtra("name", bianInfo);
				startActivity(intent);
			}
		});
	}

	private void modifyButton() {
		detailButton = (Button) findViewById(R.id.zhanbu_button_detail);
		startButton = (Button) findViewById(R.id.zhanbu_button_start);

		startButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				benHeadText.setText(R.string.zhanbu_ben_head);
				bianHeadText.setText(R.string.zhanbu_bian_head);

				textView.setText("\n");
				guaList.clear();
				for (int i = 0; i < 6; i++) {
					int temp = CommonTool.getYinYang(Math.random());
					guaList.add(temp);
					textView.setText("\n" + CommonTool.getYinYangName(temp)
							+ textView.getText());
				}
				Log.i("test", "the gua guaList :    " + guaList);
				benInfo = setGuaImage(guaList, benUpImage, benDownImage);
				ArrayList<Integer> listBian = getBianGua(guaList);
				bianInfo = setGuaImage(listBian, bianUpImage, bianDownImage);
				startButton.setText(R.string.zhanbu_button_restart);
				detailButton.setVisibility(Button.VISIBLE);

			}
		});
		detailButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent intent = new Intent(ZhanbuActivity.this,
						ZhanBuResActivity.class);
				intent.putExtra("benInfo", benInfo);
				intent.putExtra("bianInfo", bianInfo);
				intent.putExtra("guaList", guaList);
				startActivity(intent);

			}
		});
	}

	private void modifyText() {
		textView = (TextView) findViewById(R.id.zhanbu_text_result);
		benHeadText = (TextView) findViewById(R.id.zhanbu_gua_ben_head);
		bianHeadText = (TextView) findViewById(R.id.zhanbu_gua_bian_head);
	}

	private void modifyImage() {
		benUpImage = (ImageView) findViewById(R.id.zhanbu_image_bengua_up);
		benDownImage = (ImageView) findViewById(R.id.zhanbu_image_bengua_down);
		bianUpImage = (ImageView) findViewById(R.id.zhanbu_image_biangua_up);
		bianDownImage = (ImageView) findViewById(R.id.zhanbu_image_biangua_down);
	}

	private String setGuaImage(ArrayList<Integer> list, ImageView viewUp,
			ImageView viewDown) {
		String downGua = getGuaName(list, 0, 2);
		CommonTool.setImg(downGua, viewDown);
		String upGua = getGuaName(list, 3, 5);
		CommonTool.setImg(upGua, viewUp);
		String temp = upGua + "上" + downGua + "下";
		guaDao = guaDao.open();
		temp = guaDao.findNameByType(temp);
		guaDao.close();
		return temp;

	}

	private String getGuaName(ArrayList<Integer> list, int begin, int end) {
		int j = 0;
		int result = 0;
		for (int i = begin; i <= end; i++) {
			result = result + list.get(i) % 2 * (int) Math.pow(2, j++);
		}
		String res;
		switch (result) {
		case 0:
			res = "坤";
			break;
		case 1:
			res = "震";
			break;
		case 2:
			res = "坎";
			break;
		case 3:
			res = "兑";
			break;
		case 4:
			res = "艮";
			break;
		case 5:
			res = "离";
			break;
		case 6:
			res = "巽";
			break;
		case 7:
			res = "乾";
			break;
		default:
			res = null;
			break;
		}
		return res;
	}

	private ArrayList<Integer> getBianGua(ArrayList<Integer> list) {
		ArrayList<Integer> result = new ArrayList<Integer>();
		for (Integer i : list) {
			if (i == 6) {
				i = 7;
			}
			if (i == 9) {
				i = 8;
			}
			result.add(i);
		}
		return result;
	}

}
